package work4_mybatis;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import net.sf.jsqlparser.expression.operators.relational.IsNullExpression;

public class Test {
	@org.junit.Test
	public void t1() throws IOException {
		String resource = "work4_mybatis/mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = sqlSessionFactory.openSession();
		//新增
		User user =new User();
		user.setUsername("闫帅");
		user.setPassword("qweasd");
		int isInsert = session.insert("userMapper.insertUser",user);
		if(isInsert==1) {
			System.out.println("插入成功");
		}
		session.commit();//提交
		
		
		//查询
		user = session.selectOne("userMapper.selectUserById",user.getId());//根据返回的自增id查询
		System.out.println("查询成功结果如下===>>>"+user);
		
		
		//修改
		user.setUsername("闫帅******");
		int isUpdate = session.update("userMapper.updateUserById",user);
		//对修改结果进行查询
		if(isUpdate==1) {
			user=session.selectOne("userMapper.selectUserById",user.getId());
			System.out.println("修改成功结果如下===>>>"+user);
		}
		session.commit();
		
		
		//删除
		int isDelete = session.delete("userMapper.deleteUserById",user.getId());
		if(isDelete==1) {
			System.out.println("删除成功");
			session.commit();
		}
		
		session.close();//关闭
		System.out.println("关闭连接");
	}
}
